package com.courage.crawler.boss.service;


import com.alibaba.fastjson.JSON;
import com.courage.crawler.boss.domain.mapper.TBossJobMapper;
import com.courage.crawler.boss.domain.po.TBossJob;
import com.courage.crawler.boss.service.crawler.CrawlerEngine;
import com.courage.crawler.boss.service.crawler.PageCrawlerRule;
import com.courage.crawler.boss.service.crawler.RenderCrawlerRule;
import com.courage.crawler.boss.service.crawler.SelectRegionCrawlerRule;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;



@Service
@Slf4j
public class ChromeDriveService {

    private final static Logger logger = LoggerFactory.getLogger(ChromeDriveService.class);

    @Autowired
    private TBossJobMapper tBossJobMapper;

    @Autowired
    private SelectRegionCrawlerRule selectRegionCrawlerRule;

    @Autowired
    private RenderCrawlerRule renderCrawlerRule;

    @Autowired
    private PageCrawlerRule pageCrawlerRule;

    public void zhipin(String keyWord) {
        String homePage = "https://www.zhipin.com/web/geek/job";
        CrawlerEngine crawlerEngine = new CrawlerEngine(homePage);
        crawlerEngine.registerCrawlerRule(selectRegionCrawlerRule);
        crawlerEngine.registerCrawlerRule(renderCrawlerRule);
        crawlerEngine.registerCrawlerRule(pageCrawlerRule);
        crawlerEngine.startup();
    }

    public void zhipin1(String keyWord) {
        WebDriver driver = new ChromeDriver();
        try {
            //驱动的网址
            driver.get("https://www.zhipin.com");
            //浏览器窗口变大
            driver.manage().window().maximize();
            //定位输入框元素
            WebElement txtbox = driver.findElement(By.className("ipt-search"));
            //在输入框输入文本
            txtbox.sendKeys(keyWord);
            //定位按钮元素
            WebElement btn = driver.findElement(By.cssSelector("Button[ka='search_box_index']"));
            //点击按钮
            btn.click();
            Thread.sleep(3000);

            ((ChromeDriver) driver).executeScript("window.scrollTo(0,document.body.scrollHeight - 300)");
            // 下拉到底部
            ((JavascriptExecutor) driver).executeScript("window.scrollTo(0,document.body.scrollHeight - 300)");
            List<WebElement> list = driver.findElements(By.className("job-card-wrapper"));
            //jobs
            for (WebElement webElement : list) {
                String jobName = webElement.findElement(By.className("job-name")).getText();
                String jobArea = webElement.findElement(By.className("job-area")).getText();
                String salary = webElement.findElement(By.className("salary")).getText();
                String companyName = webElement.findElement(By.className("company-name")).getText();
                TBossJob tBossJob = new TBossJob();
                tBossJob.setCompanyName(companyName);
                tBossJob.setJobArea(jobArea);
                tBossJob.setSalary(salary);
                tBossJob.setJobName(jobName);
                tBossJob.setCreateTime(new Date());
                tBossJob.setUpdateTime(new Date());
                tBossJob.setSalary(salary);
                System.out.println(JSON.toJSONString(tBossJob));
                tBossJobMapper.insertSelective(tBossJob);
            }
        } catch (Exception e) {
            log.error(" zhipin error ", e);
        } finally {
            //关闭驱动
            driver.close();
        }
    }

    public void test(String keyWord) {
        ChromeOptions option = new ChromeOptions();
        // 版本：v88以上 设置window.navigator.webdriver为false
        option.addArguments("--disable-blink-features=AutomationControlled");
        // 移除 控制文字
        option.setExperimentalOption("useAutomationExtension", false);
        option.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
        ChromeDriver driver = new ChromeDriver(option);
        try {
            //驱动的网址
            driver.get("https://bot.sannysoft.com/");

            //浏览器窗口变大
            driver.manage().window().maximize();
            Thread.sleep(20000);
        } catch (Exception e) {
            log.error(" zhipin error ", e);
        } finally {
            //关闭驱动
            driver.close();
        }
    }

}
